package top.zhmq.userprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.zhmq.commonmodule.model.User;
import top.zhmq.userprovider.dao.UserDao;

import java.util.Objects;

/**
 *
 *
 * @author Glory
 * @classname UserController
 * @create 2021-06-01 11:09
 */

@RestController
public class UserController {
    @Autowired
    UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public int register(@RequestParam(value = "upassword") String upassword,
                        @RequestParam(value = "uname") String uname,
                        @RequestParam(value = "usex") String usex,
                        @RequestParam(value = "uemail") String uemail) {
        return userDao.register(upassword, uname, usex, uemail);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public User login(@RequestParam("uname") String uname) {
        return userDao.login(uname);
    }

    @RequestMapping(value = "/active", method = RequestMethod.GET)
    public int active(@RequestParam(value = "uname") String uname,
                      @RequestParam(value = "ucode") String ucode) {
        System.out.println(uname);
        System.out.println("收到" + ucode);
        if (ucode != null) {
            String sysCode = (String) redisTemplate.boundValueOps("code_" + uname).get();
            System.out.println("系统验证码1" + sysCode);
            if (Objects.equals(sysCode, ucode)) {
                return userDao.active(uname);
            }
        }
        return 0;
    }
}